<?php



/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Game
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */
 
 
 
class Game_Plugin_Distributor_Mochi extends Game_Plugin_Distributor_Abstract
{
  protected $_type_name = 'mochi';
  /*
  `game_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  
  `distributor_type` varchar(64) NULL,
  `distributor_game_id` varchar(128) NULL,
  `distributor_gameplays` int(11) unsigned NOT NULL DEFAULT '0',
  `distributor_rating` varchar(128) NULL,

  `developer_name` varchar(128) NULL, 
  
  `user_id` int(11) unsigned NOT NULL,
  `category_id` int(11) unsigned NOT NULL DEFAULT '0',
  
  `photo_id` int(11) unsigned NOT NULL DEFAULT '0',
  
  `title` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `instructions` text NULL,
  `controls` text NULL,
  
  `keywords` varchar(255) DEFAULT NULL,
  
  `height` int(11) unsigned NOT NULL DEFAULT '0',
  `width` int(11) unsigned NOT NULL DEFAULT '0',
  
  `swf_url` varchar(255) DEFAULT NULL,
  `thumbnail_url` varchar(255) DEFAULT NULL,
  
  `screenshot1_url` varchar(255) DEFAULT NULL,
  `screenshot2_url` varchar(255) DEFAULT NULL,
  `screenshot3_url` varchar(255) DEFAULT NULL,
  `screenshot4_url` varchar(255) DEFAULT NULL,
  
  `leaderboard` tinyint(1) NOT NULL DEFAULT '0',
  `coin` tinyint(1) NOT NULL DEFAULT '0',
  `highscore_type` varchar(255) DEFAULT NULL,
  
  `status` varchar(32) NOT NULL DEFAULT 'pending',

  `launch_date` datetime NULL,

   */
  protected $_dataMap = array(
    'slug' => 'distributor_ref_slug',
    'game_tag' => 'distributor_ref_id',
    'author' => 'developer_name',
    'name' => 'title',
    'description' => 'description',
    'instructions' => 'instructions',
    'height' => 'height',
    'width' => 'width',
    'swf_url' => 'swf_url',
    'thumbnail_url' => 'thumbnail_url',
    'screen1_thumb' => 'screen1_url',
    'screen2_thumb' => 'screen2_url',
    'screen3_thumb' => 'screen3_url',
    'screen4_thumb' => 'screen4_url',
    'rating' => 'content_rating',
  );


  protected $_categoryMap = array(
    'Action'  => Game_Model_Category::TYPE_ACTION,
    'Adventure'  => Game_Model_Category::TYPE_ADVENTURE,
    'Board Game'  => Game_Model_Category::TYPE_BOARDGAME,
    'Casino'  => Game_Model_Category::TYPE_CASINO,
    'Driving'  => Game_Model_Category::TYPE_DRIVING,
    'Dress-Up'  => Game_Model_Category::TYPE_DRESSUP,
    'Fighting'  => Game_Model_Category::TYPE_FIGHTING,
    'Puzzles'  => Game_Model_Category::TYPE_PUZZLES,
    'Customize'  => Game_Model_Category::TYPE_CUSTOMIZE,
    'Shooting'  => Game_Model_Category::TYPE_SHOOTING,
    'Sports'  => Game_Model_Category::TYPE_SPORTS,
    'Other'  => Game_Model_Category::TYPE_OTHER,
    'Strategy'  => Game_Model_Category::TYPE_STRATEGY,
    'Education'  => Game_Model_Category::TYPE_EDUCATION,
    'Rhythm'  => Game_Model_Category::TYPE_RHYTHM,
    'Jigsaw'  => Game_Model_Category::TYPE_JIGSAW,
  );
  
  
  public function getFeedUrl()
  {
    $url = $this->_config['feed_url'];
    if (substr($url, -1) != '/') {
      $url .= '/';
    }
    
    if ($this->_config['publisherid']) {
      $url .= $this->_config['publisherid'] . '/';
    }
    
    if ($this->_config['category']) {
    	$url .= $this->_config['category'] . '/';
    }
    else {
    	$url .= 'all/';
    }
    
    if ($this->_config['content_rating']) {
    	$url .= $this->_config['content_rating'] . '/';
    }
    else {
    	$url .= 'all/';
    }
    
    
    //?format=json&limit=10&offset=0
    $params = array('format'=>'json');
    $params['offset'] = $this->_config['offset'] ? $this->_config['offset'] : 0;
    $params['limit'] = $this->_config['limit'] ? $this->_config['limit'] : 100;
    
    if ($this->_config['tag']) {
      $params['tag'] = $this->_config['tag'];
    }

    if ($this->_config['author']) {
      $params['author'] = $this->_config['author'];
    }
    
    $url = $url . '?' . $this->generateQuery($params);
    
    return $url;
  }
  
  public function fetchGames($url = null)
  {
    if (null === $url)
    {
      $url = $this->getFeedUrl();
    }
    //die($url);
    $feed = $this->getFeed($url);
    
    if (function_exists('json_decode')) {
      $data = json_decode($feed, true);
    }
    else {
      // Zend_Json_Exception' with message 'Illegal escape sequence ..
      $data = Zend_Json_Decoder::decode($feed, Zend_Json::TYPE_ARRAY);
    }
    
    //
    
    /*
    echo "<pre>";
    print_r($data);
    echo "</pre>";
    */
    //exit;
    
    
    $games = array();
    if (isset($data["games"]) && is_array($data["games"]))
    {
      foreach ($data["games"] as $source)
      {
        $games[] = $this->normalizeData($source);
      }
    }
    

    return $games;
    
      //  print_r($data);
    
  }
  
  
  public function normalizeData($source)
  {
    $data = parent::normalizeData($source);
    
    $categories = array_merge(array($source['category']), $source['categories']);
    $data['categories'] = $this->normalizeCategory($categories);
    
    
    if (is_array($source['controls'])) {
      $ins = array();
      foreach ($source['controls'] as $control) {
        $ins[] = "{$control[0]}: {$control[1]}";
      }
      $data['controls'] = join("\r\n", $ins);
    }

    $data['keywords'] = join(", ", $source['tags']);
    
    $data['leaderboard_enabled'] = $source['leaderboard_enabled'] == 1 ? 1 : 0;
    $data['coins_enabled'] = $source['coins_enabled'] == 1 ? 1 : 0;
    $data['achievements_enabled'] = $source['achievements_enabled'] == 1 ? 1 : 0;
    
    $data['launch_date'] = date('Y-m-d H:i:s', strtotime($source['created']));
    
    $data['status'] = $this->_config['status'];
    
    if (in_array($this->_config['content_rating'], array('everyone','teen','mature'))) {
    	$data['content_rating'] = $this->_config['content_rating'];
    }
    else {
    	$data['content_rating'] = strtolower($data['content_rating']);
    }
    
    
    return $data;
  }
  

  

}
